<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$cur_nave = 'marketing';
$setting = $this->getSetting();
$config = $this->module['config']['weisrc_dish'];

if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'marketing'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array(), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array(), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}

//市场专员
$marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
if (empty($marketing)) {
    message('您不是市场专员，无权访问！');
}
$level = pdo_fetch("SELECT * FROM " . tablename("weisrc_dish_marketinglevel") . " WHERE id = :id  ORDER BY id DESC", array(':id' => $marketing['levelid']));


$fans = $this->getFansByOpenid($from_user);
if ($this->_accountlevel == 4) {
    if (empty($fans) && !empty($nickname)) {
        $insert = array(
            'weid' => $weid,
            'from_user' => $from_user,
            'nickname' => $nickname,
            'headimgurl' => $headimgurl,
            'dateline' => TIMESTAMP
        );
        pdo_insert($this->table_fans, $insert);
    } else {
        pdo_update(
            $this->table_fans,
            array(
                'headimgurl' => $headimgurl,
                'nickname' => $nickname,
            ),
            array('id' => $fans['id'])
        );
    }
} else {
    if (empty($fans) && !empty($from_user)) {
        $insert = array(
            'weid' => $weid,
            'from_user' => $from_user,
            'agentid' => $agentid,
            'agentid2' => $agentid2,
            'agentid3' => $agentid3,
            'dateline' => TIMESTAMP
        );
        pdo_insert($this->table_fans, $insert);
    }
}
$fans = $this->getFansByOpenid($from_user);

$share_title = !empty($setting['share_title']) ? str_replace("#username#", $nickname, $setting['share_title']) : "您的朋友{$nickname}邀请您来吃饭";
$share_desc = !empty($setting['share_desc']) ? str_replace("#username#", $nickname, $setting['share_desc']) : "最新潮玩法，快来试试！";
$share_image = !empty($setting['share_image']) ? tomedia($setting['share_image']) : tomedia("../addons/weisrc_dish/icon.jpg");
$share_url = $_W['siteroot'] . 'app/' . $this->createMobileUrl('usercenter', array('agentid' => $fans['id']), true);

$is_commission = 0;
$commission_tip = "";
$agent_url = $_W['siteroot'] . 'app/' . $this->createMobileUrl('usercenter', array('agentid' => $fans['id']), true);
$qrcode_url = $this->createMobileUrl('GetQrcode',array('url'=>$agent_url)); //


$follow_title = !empty($setting['follow_title']) ? $setting['follow_title'] : "立即关注";
$follow_desc = !empty($setting['follow_desc']) ? $setting['follow_desc'] : "欢迎关注智慧点餐点击马上加入,
助力品牌推广 ";
$follow_image = !empty($setting['follow_logo']) ? tomedia($setting['follow_logo']) : tomedia(".
./addons/weisrc_dish/icon.jpg");
$tipqrcode = tomedia($setting['tipqrcode']);
$tipbtn = intval($setting['tipbtn']);
$follow_url = $setting['follow_url'];
$is_show_storeinfo = 0;


$shoptotal = pdo_fetchcolumn("SELECT COUNT(1) FROM " . tablename($this->table_stores) . " WHERE weid=:weid AND
marketingid=:marketingid AND is_show=1;", array(':weid' => $weid, 'marketingid' => $marketing['id']));
$shoptotal = intval($shoptotal);

$list = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid=:weid AND
marketingid=:marketingid;", array(':weid' => $weid, 'marketingid' => $marketing['id']));
$storeids_arr = array();
foreach ($list as $key => $value) {
    $storeids_arr[] = $value['id'];
}
$storeids = implode(",", $storeids_arr);


$zero_time = mktime(0, 0, 0);
//今日金额
$today_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND dateline>:time
 AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids});", array(':weid' => $this->_weid, ':time' => $zero_time));
$today_order_price = sprintf('%.2f', $today_order_price);

$today_order_count = pdo_fetchcolumn("SELECT count(1) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND dateline>:time
 AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids}) ", array(':weid' => $this->_weid, ':time' => $zero_time));
$today_order_count = intval($today_order_count);

$today_order_marketing = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND
dateline>:time
 AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids});", array(':weid' => $this->_weid, ':time' => $zero_time));
$today_order_marketing = sprintf('%.3f', $today_order_marketing);

//本月金额
$nowmonth_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') AND storeid in ({$storeids})", array(':weid' => $weid));
$nowmonth_order_price = sprintf('%.2f', $nowmonth_order_price);

$nowmonth_order_count = pdo_fetchcolumn("SELECT count(1) FROM " . tablename($this->table_order) . " WHERE weid=:weid
AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') AND storeid in ({$storeids})", array(':weid' => $weid));
$nowmonth_order_count = intval($nowmonth_order_count);

$nowmonth_order_marketing = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid
AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND DATE_FORMAT(FROM_UNIXTIME(dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') AND storeid in ({$storeids})", array(':weid' => $weid));
$nowmonth_order_marketing = sprintf('%.3f', $nowmonth_order_marketing);


//上月金额
$lastmonth_order_price = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE
    weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(FROM_UNIXTIME(dateline), '%Y%m' ) ) =1 AND storeid in ({$storeids})",
    array(':weid' => $weid));
$lastmonth_order_price = sprintf('%.2f', $lastmonth_order_price);

$lastmonth_order_count = pdo_fetchcolumn("SELECT count(1) FROM " . tablename($this->table_order) . " WHERE
    weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(FROM_UNIXTIME(dateline), '%Y%m' ) ) =1 AND storeid in ({$storeids})",
    array(':weid' => $weid));
$lastmonth_order_count = intval($lastmonth_order_count);

$lastmonth_order_marketing = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE
    weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND
ismerge=0 AND PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(FROM_UNIXTIME(dateline), '%Y%m' ) ) =1 AND storeid in ({$storeids})",
    array(':weid' => $weid));
$lastmonth_order_marketing = sprintf('%.3f', $lastmonth_order_marketing);
//----------------------------------------------------------------------
//所有佣金
$all_marketing_price = pdo_fetchcolumn("SELECT sum(marketingprice) FROM " . tablename($this->table_order) . " WHERE
weid=:weid AND status=3 AND (paytype=1 OR paytype=2 OR paytype=4) AND ispay=1 AND ismerge=0 AND storeid in ({$storeids})", array(':weid' => $weid));
$all_marketing_price = sprintf('%.3f', $all_marketing_price);

$check_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=0 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
$check_marketing_price = sprintf('%.2f', $check_marketing_price);

$ischeck_marketing_price = pdo_fetchcolumn("SELECT sum(price) FROM " . tablename('weisrc_dish_marketinglog') . " WHERE
weid=:weid AND status=1 AND marketingid=:marketingid", array(':weid' => $weid, ':marketingid' => $marketing['id']));
$ischeck_marketing_price = sprintf('%.2f', $ischeck_marketing_price);
$marketing_price = $all_marketing_price - $check_marketing_price - $ischeck_marketing_price;
$marketing_price = sprintf('%.3f', $marketing_price);



include $this->template($this->cur_tpl . '/marketing');